package com.mall.b2c.hessian.impl;

import javax.annotation.Resource;

import com.google.common.collect.Lists;
import com.mall.b2c.bean.FirendBean;
import com.mall.b2c.domain.intergral.IntergralDomain;
import com.mall.b2c.hessian.IntergralHessian;
import com.mall.b2c.service.IntergralService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.mall.b2c.bean.MemberBean;
import com.mall.b2c.bean.ReceiverBean;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.domain.member.ReceiverDomain;
import com.mall.b2c.hessian.MemberHessian;
import com.mall.b2c.service.MemberService;
import com.mall.b2c.service.ReceiverService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hessian.impl.BaseHessianImpl;
import com.mall.common.hibernate.Finder;

import java.util.List;

@Service("b2cMemberHessian")
public class MemberHessianImpl extends BaseHessianImpl implements MemberHessian {
	@Resource private MemberService memberService;
	@Resource private ReceiverService receiverService;
	@Resource private IntergralService intergralService;

	@Override
	public String register(String mobile, String password, String ip, Long inviterId) {
		MemberBean member = memberService.register(mobile, password, ip, inviterId);
		return objToJson(member);
	}

	@Override
	public String login(String username, String password, String ip) {
		MemberBean member = memberService.login(username, password, ip);
		return objToJson(member);
	}

	@Override
	public String getMemberInfo(Long id, String username) {
		if(id == null && StringUtils.isBlank(username)) {
			log.error("id="+id+",username="+username);
			throw new NoticeException("会员不存在");
		}
		MemberDomain member = null;
		if(id != null){
			member = memberService.find(id);
		}else{
			member = memberService.getMemberByRegex(username);
		}

		if(member == null){
			throw new NoticeException("会员不存在");
		}

		MemberBean bean = new MemberBean(member);
		IntergralDomain intergral = intergralService.getByMemberId(member.getId());
		if(intergral != null){
			bean.setIntergral(intergral.getNum());
		}
		return objToJson(bean);
	}

	@Override
	public void saveMemberInfo(MemberBean member) {
		memberService.saveMember(member);
	}

	@Override
	public boolean isExistEamil(String email, Long id) {
		return memberService.isExistEamil(email, id);
	}

	@Override
	public void modifyPwd(Long memberId, String oldPwd, String newPwd) {
		memberService.modifyPwd(memberId, oldPwd, newPwd);
	}

	@Override
	public String resetPwd(Long memberId, String newPwd) {
		try {
			memberService.resetPwd(memberId,newPwd);
		}catch (Exception e){
			if(e instanceof NoticeException){
				return e.getMessage();
			}else{
				log.error("重置密码失败member="+memberId,e);
				return "重置密码失败";
			}
		}
		return null;
	}

	@Override
	public String sendEmail(String email, Long memberId, int type) {
		return memberService.sendEmail(email, memberId, type);
	}

	@Override
	public String verifyEmail(Long memberId, String code) {
		MemberDomain member = memberService.find(memberId);
		if(member == null){
			throw new NoticeException("会员不存在");
		}
		String error = member.verifyEmailCode(3, code);
		if(StringUtils.isNotBlank(error)){
			throw new NoticeException(error);
		}
		return member.getEmail();
	}

	@Override
	public String modifyEmail(String code){
		MemberDomain member = memberService.modifyEmail(code);
		return objToJson(new MemberBean(member));
	}

	@Override
	public String sendMobileCaptcha(String mobile, String ip, Boolean isCaptcha, Long memberId, int type) {
		return memberService.sendMobileCaptcha(mobile, ip, isCaptcha, memberId, type).toString();
	}

	@Override
	public String validateCode(Long memberId, String code) {
		if(memberId != null && StringUtils.isNotBlank(code)){
			MemberDomain member = memberService.find(memberId);
			if(member != null){
				return member.verifyMobileCode(code);
			}
		}
		return null;
	}

	@Override
	public void modifyMobile(String mobile, Long memberId) {
		memberService.modifyMobile(mobile, memberId);
	}

	@Override
	public String recovery(String code) {
		return objToJson(memberService.recovery(code));
	}

	@Override
	public String getReceiverList(Long memberId) {
		return listToJson(receiverService.getRecerverList(memberId));
	}

	@Override
	public String getReceiver(Long receiverId, Long memberId) {
		ReceiverDomain receiver = receiverService.getReceiver(receiverId, memberId);
		if(receiver == null){
			throw new NoticeException("收货地址不存在");
		}
		return objToJson(new ReceiverBean(receiver));
	}

	@Override
	public String addReceiver(ReceiverBean receiver, Long memberId) {
		return objToJson(receiverService.saveReceiver(receiver, memberId));
	}

	@Override
	public void updateReceiver(ReceiverBean receiver, Long memberId) {
		receiverService.updateReceiver(receiver, memberId);
	}

	@Override
	public void deleteReceiver(Long receiverId, Long memberId) {
		receiverService.deleteReceiver(receiverId, memberId);
	}

	@Override
	public String getFavoriteGoods(Long memberId, Integer pageNumber) {
		return objToJson(memberService.getFavoriteGoods(memberId, pageNumber));
	}

	@Override
	public void addFavoriteGoods(Long[] goodsIds, Long memberId) {
		memberService.addFavoriteGoods(goodsIds, memberId);
	}

	@Override
	public void deleteFavoriteGoods(Long[] goodsIds, Long memberId) {
		memberService.deleteFavoriteGoods(goodsIds, memberId);
	}

	@Override
	public void setDefaultReceiver(Long receiverId, Long memberId) {
		receiverService.setDefault(memberId, receiverId);
	}

	@Override
	public String findFavoriteList(Long memberId, Integer count) {
		String hql = "select new com.mall.b2c.bean.GoodsBean(bean, m) from MemberDomain m join m.favoriteGoods bean where m.id=:memberId order by bean.createDate desc";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		if(count == null){
			count = 10;
		}
		finder.setMaxResults(count);
		return listToJson(finder.findHQLList());
	}

	@Override
	public String findFriends(Long memberId) {

		List<FirendBean> list = Lists.newArrayList();
 		String hql = "select new com.mall.b2c.bean.FirendBean(m, 1) from MemberDomain m  where m.inviter.id=:memberId order by m.createDate desc";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		List<FirendBean> r1 = finder.findHQLList();
		list.addAll(r1);

		hql = "select new com.mall.b2c.bean.FirendBean(m, 2) from MemberDomain m  where m.inviter.inviter.id=:memberId order by m.createDate desc";
		finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		List<FirendBean> r2 = finder.findHQLList();
		list.addAll(r2);
		return listToJson(list);
	}
}
